#!/bin/bash
###############################################################################
#       脚本名称: test_multi_format.sh
#       功能描述: 多格式配置集成测试 - 测试不同格式配置的协同工作
###############################################################################

cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1

source ../../unit/test_framework.sh
source ../../../scripts/config/config.sh

TEST_DIR=$(create_test_dir)

###############################################################################
# 测试用例
###############################################################################

# 测试: 同时加载多种格式配置
test_load_multiple_formats() {
    # 创建不同格式的配置文件
    local yaml_file="$TEST_DIR/app.yaml"
    local json_file="$TEST_DIR/database.json"
    local env_file="$TEST_DIR/server.env"
    local ini_file="$TEST_DIR/cache.ini"
    
    # YAML配置
    cat > "$yaml_file" << 'EOF'
app:
  name: MyApplication
  version: 1.0.0
  debug: true
EOF
    
    # JSON配置
    cat > "$json_file" << 'EOF'
{
  "database": {
    "host": "localhost",
    "port": 3306,
    "username": "root"
  }
}
EOF
    
    # ENV配置
    cat > "$env_file" << 'EOF'
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
SERVER_WORKERS=4
EOF
    
    # INI配置
    cat > "$ini_file" << 'EOF'
[redis]
host=localhost
port=6379
database=0
EOF
    
    # 加载所有配置
    config_init 2>/dev/null
    config_load "$yaml_file" "app" 2>/dev/null
    config_load "$json_file" "database" 2>/dev/null
    config_load "$env_file" "server" 2>/dev/null
    config_load "$ini_file" "cache" 2>/dev/null
    
    # 验证可以读取所有格式的配置
    local app_name=$(config_get "app" ".app.name" 2>/dev/null)
    assert_equals "MyApplication" "$app_name"
    
    local db_host=$(config_get "database" ".database.host" 2>/dev/null)
    assert_equals "localhost" "$db_host"
    
    local server_port=$(config_get "server" "SERVER_PORT" 2>/dev/null)
    assert_equals "8080" "$server_port"
    
    local redis_port=$(config_get "cache" "redis.port" 2>/dev/null)
    assert_equals "6379" "$redis_port"
}

# 测试: 跨格式配置引用
test_cross_format_reference() {
    local yaml_file="$TEST_DIR/main.yaml"
    local env_file="$TEST_DIR/env.env"
    
    # 创建配置文件
    echo "HOST=production.example.com" > "$env_file"
    echo "app: {host: \${HOST}}" > "$yaml_file"
    
    config_init 2>/dev/null
    config_load "$env_file" "environment" 2>/dev/null
    config_load "$yaml_file" "main" 2>/dev/null
    
    # 从ENV读取主机名
    local host_from_env=$(config_get "environment" "HOST" 2>/dev/null)
    assert_not_empty "$host_from_env"
    
    # 验证两个配置都可以独立访问
    local host_from_yaml=$(config_get "main" ".app.host" 2>/dev/null)
    assert_not_empty "$host_from_yaml"
}

# 测试: 配置格式转换场景
test_format_conversion_scenario() {
    # 模拟从YAML迁移到JSON的场景
    local yaml_file="$TEST_DIR/legacy.yaml"
    local json_file="$TEST_DIR/modern.json"
    
    # 旧的YAML配置
    cat > "$yaml_file" << 'EOF'
service:
  name: LegacyService
  port: 9000
EOF
    
    config_init 2>/dev/null
    config_load "$yaml_file" "legacy" 2>/dev/null
    
    # 读取YAML配置
    local service_name=$(config_get "legacy" ".service.name" 2>/dev/null)
    local service_port=$(config_get "legacy" ".service.port" 2>/dev/null)
    
    # 创建新的JSON配置
    echo '{}' > "$json_file"
    config_load "$json_file" "modern" 2>/dev/null
    
    # 将数据迁移到JSON
    config_set "modern" ".service.name" "$service_name" "string" 2>/dev/null
    config_set "modern" ".service.port" "$service_port" "number" 2>/dev/null
    
    # 验证迁移成功
    local migrated_name=$(config_get "modern" ".service.name" 2>/dev/null)
    assert_equals "$service_name" "$migrated_name"
}

# 测试: 配置优先级和覆盖
test_configuration_priority() {
    local default_yaml="$TEST_DIR/default.yaml"
    local override_env="$TEST_DIR/override.env"
    
    # 默认配置
    cat > "$default_yaml" << 'EOF'
app:
  debug: false
  timeout: 30
  max_connections: 100
EOF
    
    # 覆盖配置
    cat > "$override_env" << 'EOF'
APP_DEBUG=true
APP_TIMEOUT=60
EOF
    
    config_init 2>/dev/null
    config_load "$default_yaml" "default" 2>/dev/null
    config_load "$override_env" "override" 2>/dev/null
    
    # 优先使用override的值
    local debug_override=$(config_get "override" "APP_DEBUG" 2>/dev/null)
    local debug_default=$(config_get "default" ".app.debug" 2>/dev/null)
    
    assert_not_equals "$debug_override" "$debug_default"
    
    # 未覆盖的值使用默认值
    local max_conn=$(config_get "default" ".app.max_connections" 2>/dev/null)
    assert_equals "100" "$max_conn"
}

# 测试: 多环境配置管理
test_multi_environment() {
    local base_yaml="$TEST_DIR/base.yaml"
    local dev_env="$TEST_DIR/dev.env"
    local prod_ini="$TEST_DIR/prod.ini"
    
    # 基础配置
    cat > "$base_yaml" << 'EOF'
app:
  name: MyApp
  version: 1.0.0
EOF
    
    # 开发环境配置
    cat > "$dev_env" << 'EOF'
ENV=development
DB_HOST=localhost
DEBUG=true
EOF
    
    # 生产环境配置
    cat > "$prod_ini" << 'EOF'
[environment]
name=production

[database]
host=prod-db.example.com
pool_size=50
EOF
    
    config_init 2>/dev/null
    
    # 加载基础配置
    config_load "$base_yaml" "base" 2>/dev/null
    
    # 根据环境加载不同配置
    local env="dev"
    if [[ "$env" == "dev" ]]; then
        config_load "$dev_env" "env_specific" 2>/dev/null
    else
        config_load "$prod_ini" "env_specific" 2>/dev/null
    fi
    
    # 验证配置正确加载
    local app_name=$(config_get "base" ".app.name" 2>/dev/null)
    assert_equals "MyApp" "$app_name"
    
    local env_name=$(config_get "env_specific" "ENV" 2>/dev/null)
    assert_equals "development" "$env_name"
}

# 测试: 配置文件变更检测
test_multi_format_change_detection() {
    local yaml_file="$TEST_DIR/watch1.yaml"
    local json_file="$TEST_DIR/watch2.json"
    
    echo "app: {name: Original}" > "$yaml_file"
    echo '{"service":"Original"}' > "$json_file"
    
    config_init 2>/dev/null
    config_load "$yaml_file" "watch1" 2>/dev/null
    config_load "$json_file" "watch2" 2>/dev/null
    
    # 记录原始哈希
    local yaml_hash1="${CONFIG_FILE_HASH[watch1]}"
    local json_hash1="${CONFIG_FILE_HASH[watch2]}"
    
    # 修改文件
    sleep 1
    echo "app: {name: Modified}" > "$yaml_file"
    echo '{"service":"Modified"}' > "$json_file"
    
    # 重新加载
    config_reload "watch1" 2>/dev/null
    config_reload "watch2" 2>/dev/null
    
    # 验证哈希值已更新
    local yaml_hash2="${CONFIG_FILE_HASH[watch1]}"
    local json_hash2="${CONFIG_FILE_HASH[watch2]}"
    
    assert_not_equals "$yaml_hash1" "$yaml_hash2"
    assert_not_equals "$json_hash1" "$json_hash2"
}

# 测试: 大规模配置管理
test_large_scale_configuration() {
    config_init 2>/dev/null
    
    # 创建10个不同格式的配置文件
    for i in {1..3}; do
        local yaml_f="$TEST_DIR/module${i}.yaml"
        echo "module${i}: {enabled: true}" > "$yaml_f"
        config_load "$yaml_f" "module${i}_yaml" 2>/dev/null
    done
    
    for i in {1..3}; do
        local json_f="$TEST_DIR/service${i}.json"
        echo "{\"service${i}\":\"active\"}" > "$json_f"
        config_load "$json_f" "service${i}_json" 2>/dev/null
    done
    
    for i in {1..2}; do
        local env_f="$TEST_DIR/env${i}.env"
        echo "ENV${i}_VAR=value${i}" > "$env_f"
        config_load "$env_f" "env${i}" 2>/dev/null
    done
    
    for i in {1..2}; do
        local ini_f="$TEST_DIR/cache${i}.ini"
        echo "[cache${i}]" > "$ini_f"
        echo "enabled=yes" >> "$ini_f"
        config_load "$ini_f" "cache${i}" 2>/dev/null
    done
    
    # 验证所有配置都已加载
    local count=${#CONFIG_FILES[@]}
    assert_true "$(( count >= 10 ))"
}

###############################################################################
# 运行测试
###############################################################################

test_suite_start "多格式配置集成测试"

run_test "同时加载多种格式配置" test_load_multiple_formats
run_test "跨格式配置引用" test_cross_format_reference
run_test "配置格式转换场景" test_format_conversion_scenario
run_test "配置优先级和覆盖" test_configuration_priority
run_test "多环境配置管理" test_multi_environment
run_test "配置文件变更检测" test_multi_format_change_detection
run_test "大规模配置管理" test_large_scale_configuration

test_suite_end

cleanup_test_dir "$TEST_DIR"

test_report
exit $?

